class Request {
  constructor(options = {}) {
    // 请求的根路径
    this.baseUrl = options.baseUrl || ''
    // 请求的 url 地址
    this.url = options.url || ''
    // 请求方式
    this.method = 'GET'
    // 请求方式
    this.responseType = 'text'
    // 请求的参数对象
    this.data = null
    // header 请求头
    this.header = options.header || {}
    this.beforeRequest = null
    this.afterRequest = null
    this.successRequest = null
    this.failRequest = null
  }

  get(url, data = {}, responseType = "text") {
    this.method = 'GET'
    this.url = this.baseUrl + url
    this.data = data
    this.responseType = responseType
    return this._()
  }

  post(url, data = {}) {
    this.method = 'POST'
    this.url = this.baseUrl + url
    this.data = data
    return this._()
  }

  put(url, data = {}) {
    this.method = 'PUT'
    this.url = this.baseUrl + url
    this.data = data
    return this._()
  }

  delete(url, data = {}) {
    this.method = 'DELETE'
    this.url = this.baseUrl + url
    this.data = data
    return this._()
  }

  _() {
    // 清空 header 对象
    this.header = {}
    // 请求之前做一些事
    this.beforeRequest && typeof this.beforeRequest === 'function' && this.beforeRequest(this)
    // 发起请求
    return new Promise((resolve, reject) => {
      let weixin = wx
      // 适配 uniapp
      if ('undefined' !== typeof uni) {
        weixin = uni
      }
      let shenai_userToken = wx.getStorageSync('shenai_userToken');
      if (shenai_userToken.token)
        this.header = {
          "X-WX-SERVICE": "shenai",
          'token': shenai_userToken.token
        }
      else this.header = {
        "X-WX-SERVICE": "shenai"
      }




      wx.cloud.callContainer({
        "config": {
          "env": "prod-2gqzz1rq19c530a4"
        },
        "path": this.url,
        "header": this.header,
        "method": this.method,
        "responseType": this.responseType,
        "data": this.data,
        success: (res) => {
          this.successRequest && typeof this.successRequest === 'function' && this.successRequest(res);
          resolve(res)
        },
        fail: (err) => {
          this.failRequest && typeof this.failRequest === 'function' && this.failRequest(res)
          reject(err)
        },
        complete: (res) => {
          // 请求完成以后做一些事情
          this.afterRequest && typeof this.afterRequest === 'function' && this.afterRequest(res)
        }
      })



      // weixin.request({
      //   url: this.url,
      //   method: this.method,
      //   data: this.data,
      //   responseType: this.responseType,
      //   header: this.header,
      //   success: (res) => {
      //     this.successRequest && typeof this.successRequest === 'function' && this.successRequest(res);
      //     resolve(res)
      //   },
      //   fail: (err) => {
      //     this.failRequest && typeof this.failRequest === 'function' && this.failRequest(res)
      //     reject(err)
      //   },
      //   complete: (res) => {
      //     // 请求完成以后做一些事情
      //     this.afterRequest && typeof this.afterRequest === 'function' && this.afterRequest(res)
      //   }
      // })
    })
  }
}

export const $http = new Request()